package com.trademark.script.methods;

import java.awt.Image;
import java.awt.image.RenderedImage;
import java.io.File;
import java.net.URL;

import javax.imageio.ImageIO;

import com.rsbuddy.script.methods.Environment;

public class Img {
	/*
	 * @param fileName The name given to the file being saved
	 * 
	 * @param imageURL The image's url to be saved
	 */
	public static Image getImage(String fileName, String imageURL) {
		String myDir = "TMScripts";
		File myDirectory = new File(Environment.getStorageDirectory()
				+ File.separator + myDir);
		if (!myDirectory.exists()) {
			myDirectory.mkdir();
			getImage(fileName, imageURL);
		}
		try {
			File file = new File(Environment.getStorageDirectory()
					+ File.separator + myDir + File.separator + fileName);
			if (!file.exists()) {
				Image image = null;
				URL url = new URL(imageURL);
				image = ImageIO.read(url);
				ImageIO.write((RenderedImage) image, "PNG", file);
				return image;
			}
			Image img = ImageIO.read(file);
			return img;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}